package com.app.abby.perfectweather.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.app.abby.perfectweather.R;
import com.app.abby.perfectweather.base.BaseActivity;
import com.app.abby.perfectweather.model.api.ApiClient;
import com.app.abby.perfectweather.model.api.SwitchClient;
import com.app.abby.perfectweather.model.api.switchBean;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by Abby on 9/2/2017.
 */

public class LaunchActivity extends AppCompatActivity {

    private Subscription subscription;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);


        subscription=Observable.timer(1, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Long>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Long aLong) {

                        SwitchClient.getInstance().getEntrance()
                                .subscribe(new Subscriber<switchBean>() {
                                    @Override
                                    public void onCompleted() {

                                    }

                                    @Override
                                    public void onError(Throwable throwable) {

                                    }

                                    @Override
                                    public void onNext(switchBean switchBean) {
                                        switch (switchBean.getCode()){
                                            case 1://网页
                                                Intent intent=new Intent(LaunchActivity.this,WebActivity.class)
                                                        .putExtra("web_url", switchBean.getWebUrl())
                                                        .putExtra("isTitle", false);
                                                startActivity(intent);
                                                finish();
                                                overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
                                                break;
                                            case 2://更新
                                                break;
                                            case 0://自己页面
                                                Intent intent2=new Intent(LaunchActivity.this,MainActivity.class);
                                                startActivity(intent2);
                                                finish();
                                                overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
                                                break;
                                        }
                                    }
                                });

                    }
                });

    }


    @Override
    public void onDestroy(){
        super.onDestroy();
        if(subscription!=null&&!subscription.isUnsubscribed())
            subscription.unsubscribe();
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        if (hasFocus&& Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }

    }


}
